package com.wxy.school.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxy.school.common.R;
import com.wxy.school.dto.RepairOrdersDto;
import com.wxy.school.entity.RepairOrders;
import com.wxy.school.service.RepairOrdersService;
import com.wxy.school.utils.BaseContext;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author 王祥宇
 * @projectName school
 * @packageName com.wxy.school.controller
 * @data 2023/3/26 17:45
 * @description: TODO
 * @version: 1.0
 */
@RestController
@RequestMapping("school/repairOrders")
@Slf4j
public class RepairOrdersController {

    @Resource
    private RepairOrdersService repairOrdersService;

    /**
     * @description: 提交报修登记信息
     * @author: 王祥宇
     * @param: repairOrdersDto
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @PostMapping("/addOrders")
    public R<String> add(@RequestBody RepairOrdersDto repairOrdersDto){
        BaseContext.setThreadLocal(repairOrdersDto.getUserName());
        RepairOrders repairOrders = new RepairOrders();
        BeanUtils.copyProperties(repairOrdersDto, repairOrders, "userName");
        repairOrdersService.save(repairOrders);
        return R.success("提交成功");
    }

    @GetMapping("/getOrdersList")
    public R<Page> get(int pageNum, int pageSize, String qurey){
        Page<RepairOrders> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<RepairOrders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(qurey), RepairOrders::getId, qurey);
        lambdaQueryWrapper.orderByDesc(RepairOrders::getCreateTime);
        repairOrdersService.page(page, lambdaQueryWrapper);
        return R.success(page);
    }

    @PutMapping("/updateOrders")
    public R<String> update(@RequestBody RepairOrdersDto repairOrdersDto){
        RepairOrders repairOrders = new RepairOrders();
        BeanUtils.copyProperties(repairOrdersDto, repairOrders, "userName");
        BaseContext.setThreadLocal(repairOrdersDto.getUserName());
        repairOrdersService.updateById(repairOrders);
        return R.success("订单状态已更新");
    }
}
